package com.example.config;

import com.example.utils.FastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/*
 *  @author 雨安
 *  类名： RedisConfig
 *  创建时间：2024/3/18 Redis配置类
 */
@Configuration
public class RedisConfig {
    @Bean
    @SuppressWarnings(value = {"unchecked","rawtypes"})
    public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory connectionFactory){
        //创建对象
        RedisTemplate<Object,Object> template = new RedisTemplate<>();
        //设置工厂
        template.setConnectionFactory(connectionFactory);
        //创建序列化
        FastJsonRedisSerializer serializer = new FastJsonRedisSerializer(Object.class);
        //施一公StringRedisSerializer来序列化和返序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(serializer);

        //Hash的key也采用StringRedisSerializer的序列化方式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();
        //返回对象
        return template;
    }



}
